<?php
// Controller/Default.php

class Controller_Sentence extends FLEA_Controller_Action
{
    function actionindex(){
        // $smarty =& $this->_getView();
        // $smarty->display('index.html');
    }
    function actionlistbyword(){
        $smarty =& $this->_getView();
        $word = $_GET['word'];
        if(empty($word)){
            echo 'please input the word';
            exit;
        }
        $table_sentences =& FLEA::getSingleton('Table_wordsentence');
        $sentences = $table_sentences->findAll(array('Word'=>$word,'status'=>1),'rank');
        foreach($sentences as $key => &$value){
            $value['Chi_Sen'] = iconv("GB18030","UTF-8",$value['Chi_Sen']);
        }
        $smarty->assign('word', $word);
        $smarty->assign('sentences', $sentences);
        $smarty->display('search_sentencelist.html');
    }
    function actioncreate(){
        if($this->_isPOST()){
            $row['Word'] = $_POST['word'];
            if(empty($row['Word'])){
                js_alert('please input the word','history.back();');
            }
            $row['rank'] = (int)$_POST['rank'];
            $row['En_Sen'] = $_POST['en_sen'];
            $row['Chi_Sen'] = iconv("UTF-8","GB2312",$_POST['chi_sen']);
            $table_sentence = & FLEA::getSingleton('Table_wordsentence');
            $no = $table_sentence->findCount(array('Word'=>$row['Word']));
            $row['No'] = ++$no;
            if($table_sentence->create($row)){
                js_alert('更新成功','history.back();');
            }
            else{
                js_alert('更新失败，请重试或联系管理员','history.back();');
            }
        }
        else{
            $smarty =& $this->_getView();
            $smarty->display('createsentence.html');
        }
    }
    function actionedit(){
        if($this->_isPOST()){
            $row['id'] = (int)$_POST['id'];
            $row['Word'] = $_POST['word'];
            $row['No'] = (int)$_POST['no'];
            $row['rank'] = (int)$_POST['rank'];
            $row['En_Sen'] = $_POST['en_sen'];
            $row['Chi_Sen'] = $_POST['chi_sen'];
            $row['Chi_Sen'] = iconv("UTF-8","GB18030",$row['Chi_Sen']);
            $table_sentence = & FLEA::getSingleton('Table_wordsentence');
            if($table_sentence->save($row)){
                js_alert('更新成功','history.back();');
            }
            else{
                js_alert('更新失败，请重试或联系管理员','history.back();');
            }
        }
        else{
            $id = $_GET['id'];
            if(empty($id)){
                echo 'please input the word';
            }
            $smarty =& $this->_getView();
            $table_sentence =& FLEA::getSingleton('Table_wordsentence');
            $sentence = $table_sentence->find((int)$id);
            $sentence['Chi_Sen'] = trim(iconv("GB18030","UTF-8",$sentence['Chi_Sen']));
            $smarty->assign('data', $sentence);
            $smarty->display('editsentence.html');
        }
    }
   function actionlist(){
        $smarty =& $this->_getView();
        $page= (int)$_GET['page'];
        if(empty($page)){
            $page = 1; 
        }
        $table_sentences =& FLEA::getSingleton('Table_wordsentence');
        FLEA::loadHelper('Pager');
        $pager =& new FLEA_Helper_Pager($table_sentences,$page,20,array('status'=>1),null,1);

        // select the word category
        $worddb = $_SESSION['word_db'];
        if(!empty($worddb)){
            $table_words =& FLEA::getSingleton('Table_'.$worddb);
            $words = $table_words->findAll(null,null,null,'Word');
            foreach($words as & $value){
                $value = $value['Word'];
            }
            $pager->_conditions = array('in()'=>array('Word'=>$words),'status'=>1);
            $pager->setCount($table_sentences->findCount($pager->_conditions));
        }

        $data = $pager->findAll();
        $data_page = $pager->getPagerData();
        unset($data_page['pagesNumber']);
        foreach($data as $key => &$value){
            $value['Chi_Sen'] = iconv("GB18030","UTF-8",$value['Chi_Sen']);
        }
        $smarty->assign('data', $data);
        $smarty->assign('data_page', $data_page);
        $smarty->display('sentencelist.html');
    }
    function actiondel(){
        $smarty =& $this->_getView();
        $row['id']= (int)$_GET['id'];
        if(empty($row['id'])){
            js_alert("链接错误!",'history.back();');
        }
        $table_sentences =& FLEA::getSingleton('Table_wordsentence');
        $row['status'] = 0;
        if($table_sentences->save($row)){
            js_alert('成功移除到回收站','history.back();');
        }
        else{
            js_alert('出错了，请联系管理员','history.back();');
        }
    }
   function actionbackfromtrash(){
        $smarty =& $this->_getView();
        $row['id']= (int)$_GET['id'];
        if(empty($row['id'])){
            js_alert("链接错误!",'history.back();');
        }
        $table_sentences =& FLEA::getSingleton('Table_wordsentence');
        $row['status'] = 1;
        if($table_sentences->save($row)){
            js_alert('从回收站中还原!','history.back();');
        }
        else{
            js_alert('出错了，请联系管理员','history.back();');
        }
    }
    function actionlistintrash(){
        $smarty =& $this->_getView();
        $page= (int)$_GET['page'];
        if(empty($page)){
            $page = 1; 
        }
        $table_sentences =& FLEA::getSingleton('Table_wordsentence');
        FLEA::loadHelper('Pager');
        $pager =& new FLEA_Helper_Pager($table_sentences,$page,20,array('status'=>0),null,1);
        $data = $pager->findAll();
        $data_page = $pager->getPagerData();
        unset($data_page['pagesNumber']);
        foreach($data as $key => &$value){
            $value['Chi_Sen'] = iconv("GB18030","UTF-8",$value['Chi_Sen']);
        }
        $smarty->assign('data', $data);
        $smarty->assign('data_page', $data_page);
        $smarty->display('sentenceintrash.html');
    }
    function actiondelintrash(){
        $smarty =& $this->_getView();
        $row['id']= (int)$_GET['id'];
        if(empty($row['id'])){
            js_alert("链接错误!",'history.back();');
        }
        $table_sentences =& FLEA::getSingleton('Table_wordsentence');
        if($table_sentences->remove($row)){
            js_alert('已从回收站中删除!','history.back();');
        }
        else{
            js_alert('出错了，请联系管理员','history.back();');
        }
    }
    function actionvoice(){
        $word = $_GET[word];
        $no = $_GET[no];
        $table_voice = & FLEA::getSingleton('Table_wordsentence');
        $voice = $table_voice->find(array('Word'=>$word,'No'=>$no));
        Header("Content-type:audio/mp3"); 
        print $voice[voicedata];
    }
  }
